package cc.miwuya.community.controller;

import cc.miwuya.common.util.Result;
import cc.miwuya.community.domain.Residential;
import cc.miwuya.community.service.ResidentialService;
import cc.miwuya.core.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 迷雾森林
 * @Date 2024-02-22 20:23
 * @Desc 社区操作小区控制器
 */
@RestController
@RequestMapping("/community/residential")
public class ResidentialController extends BaseController {

    @Resource
    ResidentialService residentialService;

    /**
     * 获取小区列表
     * @return
     */
    @GetMapping("/list")
    public Result list(String name){
        return Result.success(residentialService.getPage(getPage(),name));
    }

    /**
     * 添加或修改小区
     * @param residential
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Residential residential){
        residentialService.saveOrUpdate(residential);
        return Result.success();
    }

    /**
     * 获取小区信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable Long id){
        return Result.success(residentialService.getById(id));
    }

    /**
     * 根据ID删除小区
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        return Result.success(residentialService.removeById(id));
    }

    /**
     * 根据物业id获取所有管理的小区（分页）
     * @param id
     * @return
     */
    @GetMapping("/list/property/{id}")
    public Result getResidentialListByPropertyId(@PathVariable Long id){
        return Result.success(residentialService.getListByPropertyId(getPage(),id));
    }
    @GetMapping("/list/propertyId/{id}")
    public List<Long> getResidentialIdByPropertyId(@PathVariable Long id){
        return residentialService.listObjs(new QueryWrapper<Residential>().eq("property_id",id).select("id"));
    }
    @GetMapping("/getAllResidential")
    public Result getResidentialList(){
        return Result.success(residentialService.list());
    }
}
